package com.youxue.teachin.set.usercase;

import java.util.List;

import com.library.rest.ApiServiceManager;
import com.library.rest.rxjava.RxResultHelper;
import com.library.rest.rxjava.RxSchedulersHelper;
import com.library.rest.vo.ResponseModel;
import com.youxue.teachin.common.vo.UserVO;
import com.youxue.teachin.set.main.maper.SetMaper;
import com.youxue.teachin.set.repository.ISetRepository;
import com.youxue.teachin.user.userSystem.UserSystem;
import rx.Observable;
import rx.functions.Func1;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-05 23:02
 * @version: 9.1.0
 */
public class SetUserCase {
    final ISetRepository mRepository;

    public SetUserCase() {
        Class<ISetRepository> clazz = ISetRepository.class;
        mRepository = ApiServiceManager.getOrCreateApiService(clazz);
    }

    public Observable<UserVO> getUserInfo() {
        return mRepository.getUserInfo()
            .compose(RxSchedulersHelper.<ResponseModel<UserVO>>io_main())
            .compose(RxResultHelper.<UserVO>handleResult())
            .map(new Func1<UserVO, UserVO>() {
                @Override
                public UserVO call(UserVO userVO) {
                    UserSystem.getInstance().updateUser(userVO);
                    return userVO;
                }
            });
    }

    public Observable<List> getUserInfoList() {
        return mRepository.getUserInfo()
            .compose(RxSchedulersHelper.<ResponseModel<UserVO>>io_main())
            .compose(RxResultHelper.<UserVO>handleResult())
            .map(new Func1<UserVO, List>() {
                @Override
                public List call(UserVO userVO) {
                    UserSystem.getInstance().updateUser(userVO);
                    return SetMaper.transformSet(userVO);
                }

            });


    }
}
